home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / foo / altos-dep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-10  |  14.4 KB  |  586 lines

  1. /* Low level interface to ptrace, for GDB when running under m68k SVR2 Unix
  2.    on Altos 3068.  Report bugs to Jyrki Kuoppala <jkp@cs.hut.fi>
  3.    Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. GDB is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GDB is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GDB; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include "defs.h"
  23. #include "param.h"
  24. #include "frame.h"
  25. #include "inferior.h"
  26.  
  27. #ifdef USG
  28. #include <sys/types.h>
  29. #endif
  30.  
  31. #include <sys/param.h>
  32. #include <sys/dir.h>
  33. #include <signal.h>
  34. #include <sys/ioctl.h>
  35. #include <fcntl.h>
  36. #ifdef USG
  37. #include <sys/page.h>
  38. #ifdef ALTOS
  39. #include <sys/net.h>
  40. #include <errno.h>
  41. #endif
  42. #endif
  43.  
  44. #ifdef COFF_ENCAPSULATE
  45. #include "a.out.encap.h"
  46. #else
  47. #include <a.out.h>
  48. #endif
  49. #ifndef N_SET_MAGIC
  50. #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
  51. #endif
  52.  
  53. #include <sys/user.h>        /* After a.out.h  */
  54. #include <sys/file.h>
  55. #include <sys/stat.h>
  56.  
  57. extern int errno;
  58.  
  59. /* This function simply calls ptrace with the given arguments.  
  60.    It exists so that all calls to ptrace are isolated in this 
  61.    machine-dependent file. */
  62. int
  63. call_ptrace (request, pid, arg3, arg4)
  64.      int request, pid, arg3, arg4;
  65. {
  66.   return ptrace (request, pid, arg3, arg4);
  67. }
  68.  
  69. kill_inferior ()
  70. {
  71.   if (remote_debugging)
  72.     return;
  73.   if (inferior_pid == 0)
  74.     return;
  75.   ptrace (8, inferior_pid, 0, 0);
  76.   wait (0);
  77.   inferior_died ();
  78. }
  79.  
  80. /* This is used when GDB is exiting.  It gives less chance of error.*/
  81.  
  82. kill_inferior_fast ()
  83. {
  84.   if (remote_debugging)
  85.     return;
  86.   if (inferior_pid == 0)
  87.     return;
  88.   ptrace (8, inferior_pid, 0, 0);
  89.   wait (0);
  90. }
  91.  
  92. /* Resume execution of the inferior process.
  93.    If STEP is nonzero, single-step it.
  94.    If SIGNAL is nonzero, give it that signal.  */
  95.  
  96. void
  97. resume (step, signal)
  98.      int step;
  99.      int signal;
  100. {
  101.   errno = 0;
  102.   if (remote_debugging)
  103.     remote_resume (step, signal);
  104.   else
  105.     {
  106.       ptrace (step ? 9 : 7, inferior_pid, 1, signal);
  107.       if (errno)
  108.     perror_with_name ("ptrace");
  109.     }
  110. }
  111.  
  112. void
  113. fetch_inferior_registers ()
  114. {
  115.   register int regno;
  116.   register unsigned int regaddr;
  117.   char buf[MAX_REGISTER_RAW_SIZE];
  118.   register int i;
  119.  
  120.   struct user u;
  121.   unsigned int offset = (char *) &u.u_state - (char *) &u;
  122.   offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  123.  
  124.   for (regno = 0; regno < NUM_REGS; regno++)
  125.     {
  126.       regaddr = register_addr (regno, offset);
  127.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  128.      {
  129.        *(int *) &buf[i] = ptrace (3, inferior_pid, regaddr, 0);
  130.        regaddr += sizeof (int);
  131.      }
  132.       supply_register (regno, buf);
  133.     }
  134. }
  135.  
  136. /* Store our register values back into the inferior.
  137.    If REGNO is -1, do this for all registers.
  138.    Otherwise, REGNO specifies which register (so we can save time).  */
  139.  
  140. store_inferior_registers (regno)
  141.      int regno;
  142. {
  143.   register unsigned int regaddr;
  144.   char buf[80];
  145.  
  146.   struct user u;
  147.   unsigned int offset = (char *) &u.u_state - (char *) &u;
  148.   offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  149.  
  150.   if (regno >= 0)
  151.     {
  152.       regaddr = register_addr (regno, offset);
  153.       errno = 0;
  154.       ptrace (6, inferior_pid, regaddr, read_register (regno));
  155.       if (errno != 0)
  156.     {
  157.       sprintf (buf, "writing register number %d", regno);
  158.       perror_with_name (buf);
  159.     }
  160.     }
  161.   else for (regno = 0; regno < NUM_REGS; regno++)
  162.     {
  163.       regaddr = register_addr (regno, offset);
  164.       errno = 0;
  165.       ptrace (6, inferior_pid, regaddr, read_register (regno));
  166.       if (errno != 0)
  167.     {
  168.       sprintf (buf, "writing all regs, number %d", regno);
  169.       perror_with_name (buf);
  170.     }
  171.     }
  172. }
  173.  
  174. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  175.    to debugger memory starting at MYADDR. 
  176.    On failure (cannot read from inferior, usually because address is out
  177.    of bounds) returns the value of errno. */
  178.  
  179. int
  180. read_inferior_memory (memaddr, myaddr, len)
  181.      CORE_ADDR memaddr;
  182.      char *myaddr;
  183.      int len;
  184. {
  185.   register int i;
  186.   /* Round starting address down to longword boundary.  */
  187.   register CORE_ADDR addr = memaddr & - sizeof (int);
  188.   /* Round ending address up; get number of longwords that makes.  */
  189.   register int count
  190.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  191.   /* Allocate buffer of that many longwords.  */
  192.   register int *buffer = (int *) alloca (count * sizeof (int));
  193.   extern int errno;
  194.  
  195.   /* Read all the longwords */
  196.   for (i = 0; i < count; i++, addr += sizeof (int))
  197.     {
  198.       errno = 0;
  199.       if (remote_debugging)
  200.     buffer[i] = remote_fetch_word (addr);
  201.       else
  202.     buffer[i] = ptrace (1, inferior_pid, addr, 0);
  203.       if (errno)
  204.     return errno;
  205.     }
  206.  
  207.   /* Copy appropriate bytes out of the buffer.  */
  208.   bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  209.   return 0;
  210. }
  211.  
  212. /* Copy LEN bytes of data from debugger memory at MYADDR
  213.    to inferior's memory at MEMADDR.
  214.    On failure (cannot write the inferior)
  215.    returns the value of errno.  */
  216.  
  217. int
  218. write_inferior_memory (memaddr, myaddr, len)
  219.      CORE_ADDR memaddr;
  220.      char *myaddr;
  221.      int len;
  222. {
  223.   register int i;
  224.   /* Round starting address down to longword boundary.  */
  225.   register CORE_ADDR addr = memaddr & - sizeof (int);
  226.   /* Round ending address up; get number of longwords that makes.  */
  227.   register int count
  228.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  229.   /* Allocate buffer of that many longwords.  */
  230.   register int *buffer = (int *) alloca (count * sizeof (int));
  231.   extern int errno;
  232.  
  233.   /* Fill start and end extra bytes of buffer with existing memory data.  */
  234.  
  235.   if (remote_debugging)
  236.     buffer[0] = remote_fetch_word (addr);
  237.   else
  238.     buffer[0] = ptrace (1, inferior_pid, addr, 0);
  239.  
  240.   if (count > 1)
  241.     {
  242.       if (remote_debugging)
  243.     buffer[count - 1]
  244.       = remote_fetch_word (addr + (count - 1) * sizeof (int));
  245.       else
  246.     buffer[count - 1]
  247.       = ptrace (1, inferior_pid,
  248.             addr + (count - 1) * sizeof (int), 0);
  249.     }
  250.  
  251.   /* Copy data to be written over corresponding part of buffer */
  252.  
  253.   bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  254.  
  255.   /* Write the entire buffer.  */
  256.  
  257.   for (i = 0; i < count; i++, addr += sizeof (int))
  258.     {
  259.       errno = 0;
  260.       if (remote_debugging)
  261.     remote_store_word (addr, buffer[i]);
  262.       else
  263.     ptrace (4, inferior_pid, addr, buffer[i]);
  264.       if (errno)
  265.     return errno;
  266.     }
  267.  
  268.   return 0;
  269. }
  270.  
  271. /* Work with core dump and executable files, for GDB. 
  272.    This code would be in core.c if it weren't machine-dependent. */
  273.  
  274. #ifndef N_TXTADDR
  275. #define N_TXTADDR(hdr) 0
  276. #endif /* no N_TXTADDR */
  277.  
  278. #ifndef N_DATADDR
  279. #define N_DATADDR(hdr) hdr.a_text
  280. #endif /* no N_DATADDR */
  281.  
  282. /* Make COFF and non-COFF names for things a little more compatible
  283.    to reduce conditionals later.  */
  284.  
  285. #ifdef COFF_FORMAT
  286. #define a_magic magic
  287. #endif
  288.  
  289. #ifndef COFF_FORMAT
  290. #ifndef AOUTHDR
  291. #define AOUTHDR struct exec
  292. #endif
  293. #endif
  294.  
  295. extern char *sys_siglist[];
  296.  
  297.  
  298. /* Hook for `exec_file_command' command to call.  */
  299.  
  300. extern void (*exec_file_display_hook) ();
  301.    
  302. /* File names of core file and executable file.  */
  303.  
  304. extern char *corefile;
  305. extern char *execfile;
  306.  
  307. /* Descriptors on which core file and executable file are open.
  308.    Note that the execchan is closed when an inferior is created
  309.    and reopened if the inferior dies or is killed.  */
  310.  
  311. extern int corechan;
  312. extern int execchan;
  313.  
  314. /* Last modification time of executable file.
  315.    Also used in source.c to compare against mtime of a source file.  */
  316.  
  317. extern int exec_mtime;
  318.  
  319. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  320.  
  321. extern CORE_ADDR data_start;
  322. extern CORE_ADDR data_end;
  323. extern CORE_ADDR stack_start;
  324. extern CORE_ADDR stack_end;
  325.  
  326. /* Virtual addresses of bounds of two areas of memory in the exec file.
  327.    Note that the data area in the exec file is used only when there is no core file.  */
  328.  
  329. extern CORE_ADDR text_start;
  330. extern CORE_ADDR text_end;
  331.  
  332. extern CORE_ADDR exec_data_start;
  333. extern CORE_ADDR exec_data_end;
  334.  
  335. /* Address in executable file of start of text area data.  */
  336.  
  337. extern int text_offset;
  338.  
  339. /* Address in executable file of start of data area data.  */
  340.  
  341. extern int exec_data_offset;
  342.  
  343. /* Address in core file of start of data area data.  */
  344.  
  345. extern int data_offset;
  346.  
  347. /* Address in core file of start of stack area data.  */
  348.  
  349. extern int stack_offset;
  350.  
  351. #ifdef COFF_FORMAT
  352. /* various coff data structures */
  353.  
  354. extern FILHDR file_hdr;
  355. extern SCNHDR text_hdr;
  356. extern SCNHDR data_hdr;
  357.  
  358. #endif /* not COFF_FORMAT */
  359.  
  360. /* a.out header saved in core file.  */
  361.   
  362. extern AOUTHDR core_aouthdr;
  363.  
  364. /* a.out header of exec file.  */
  365.  
  366. extern AOUTHDR exec_aouthdr;
  367.  
  368. extern void validate_files ();
  369.  
  370. core_file_command (filename, from_tty)
  371.      char *filename;
  372.      int from_tty;
  373. {
  374.   int val;
  375.   extern char registers[];
  376.  
  377.   /* Discard all vestiges of any previous core file
  378.      and mark data and stack spaces as empty.  */
  379.  
  380.   if (corefile)
  381.     free (corefile);
  382.   corefile = 0;
  383.  
  384.   if (corechan >= 0)
  385.     close (corechan);
  386.   corechan = -1;
  387.  
  388.   data_start = 0;
  389.   data_end = 0;
  390.   stack_start = STACK_END_ADDR;
  391.   stack_end = STACK_END_ADDR;
  392.  
  393.   /* Now, if a new core file was specified, open it and digest it.  */
  394.  
  395.   if (filename)
  396.     {
  397.       filename = tilde_expand (filename);
  398.       make_cleanup (free, filename);
  399.       
  400.       if (have_inferior_p ())
  401.     error ("To look at a core file, you must kill the inferior with \"kill\".");
  402.       corechan = open (filename, O_RDONLY, 0);
  403.       if (corechan < 0)
  404.     perror_with_name (filename);
  405.       /* 4.2-style (and perhaps also sysV-style) core dump file.  */
  406.       {
  407.     struct user u;
  408.  
  409.     unsigned int reg_offset;
  410.  
  411.     val = myread (corechan, &u, sizeof u);
  412.     if (val < 0)
  413.       perror_with_name ("Not a core file: reading upage");
  414.     if (val != sizeof u)
  415.       error ("Not a core file: could only read %d bytes", val);
  416.     data_start = exec_data_start;
  417.  
  418. #define NBPG NBPP
  419. #define UPAGES USIZE
  420.  
  421.     data_end = data_start + NBPG * u.u_dsize;
  422.     stack_start = stack_end - NBPG * u.u_ssize;
  423.     data_offset = NBPG * UPAGES + exec_data_start % NBPG /* Not sure about this //jkp */;
  424.     stack_offset = NBPG * (UPAGES + u.u_dsize);
  425.  
  426.     /* Some machines put an absolute address in here and some put
  427.        the offset in the upage of the regs.  */
  428.     reg_offset = (int) u.u_state;
  429.     if (reg_offset > NBPG * UPAGES)
  430.       reg_offset -= KERNEL_U_ADDR;
  431.  
  432.     bcopy (&u.u_exdata, &core_aouthdr, sizeof (AOUTHDR));
  433.     printf ("Core file is from \"%s\".\n", u.u_comm);
  434.  
  435.     /* I don't know where to find this info.
  436.        So, for now, mark it as not available.  */
  437.     N_SET_MAGIC (core_aouthdr, 0);
  438.  
  439.     /* Read the register values out of the core file and store
  440.        them where `read_register' will find them.  */
  441.  
  442.     {
  443.       register int regno;
  444.  
  445.       for (regno = 0; regno < NUM_REGS; regno++)
  446.         {
  447.           char buf[MAX_REGISTER_RAW_SIZE];
  448.  
  449.           val = lseek (corechan, register_addr (regno, reg_offset), 0);
  450.           if (val < 0
  451.           || (val = myread (corechan, buf, sizeof buf)) < 0)
  452.         {
  453.           char * buffer = (char *) alloca (strlen (reg_names[regno])
  454.                            + 30);
  455.           strcpy (buffer, "Reading register ");
  456.           strcat (buffer, reg_names[regno]);
  457.                            
  458.           perror_with_name (buffer);
  459.         }
  460.  
  461.           supply_register (regno, buf);
  462.         }
  463.     }
  464.       }
  465.       if (filename[0] == '/')
  466.     corefile = savestring (filename, strlen (filename));
  467.       else
  468.     {
  469.       corefile = concat (current_directory, "/", filename);
  470.     }
  471.  
  472.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  473.                         read_pc ()));
  474.       select_frame (get_current_frame (), 0);
  475.       validate_files ();
  476.     }
  477.   else if (from_tty)
  478.     printf ("No core file now.\n");
  479. }
  480.  
  481. exec_file_command (filename, from_tty)
  482.      char *filename;
  483.      int from_tty;
  484. {
  485.   int val;
  486.  
  487.   /* Eliminate all traces of old exec file.
  488.      Mark text segment as empty.  */
  489.  
  490.   if (execfile)
  491.     free (execfile);
  492.   execfile = 0;
  493.   data_start = 0;
  494.   data_end -= exec_data_start;
  495.   text_start = 0;
  496.   text_end = 0;
  497.   exec_data_start = 0;
  498.   exec_data_end = 0;
  499.   if (execchan >= 0)
  500.     close (execchan);
  501.   execchan = -1;
  502.  
  503.   /* Now open and digest the file the user requested, if any.  */
  504.  
  505.   if (filename)
  506.     {
  507.       filename = tilde_expand (filename);
  508.       make_cleanup (free, filename);
  509.       
  510.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  511.             &execfile);
  512.       if (execchan < 0)
  513.     perror_with_name (filename);
  514.  
  515. #ifdef COFF_FORMAT
  516.       {
  517.     int aout_hdrsize;
  518.     int num_sections;
  519.  
  520.     if (read_file_hdr (execchan, &file_hdr) < 0)
  521.       error ("\"%s\": not in executable format.", execfile);
  522.  
  523.     aout_hdrsize = file_hdr.f_opthdr;
  524.     num_sections = file_hdr.f_nscns;
  525.  
  526.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  527.       error ("\"%s\": can't read optional aouthdr", execfile);
  528.  
  529.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
  530.                   aout_hdrsize) < 0)
  531.       error ("\"%s\": can't read text section header", execfile);
  532.  
  533.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
  534.                   aout_hdrsize) < 0)
  535.       error ("\"%s\": can't read data section header", execfile);
  536.  
  537.     text_start = exec_aouthdr.text_start;
  538.     text_end = text_start + exec_aouthdr.tsize;
  539.     text_offset = text_hdr.s_scnptr;
  540.     exec_data_start = exec_aouthdr.data_start;
  541.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  542.     exec_data_offset = data_hdr.s_scnptr;
  543.     data_start = exec_data_start;
  544.     data_end += exec_data_start;
  545.     exec_mtime = file_hdr.f_timdat;
  546.       }
  547. #else /* not COFF_FORMAT */
  548.       {
  549.     struct stat st_exec;
  550.  
  551. #ifdef HEADER_SEEK_FD
  552.     HEADER_SEEK_FD (execchan);
  553. #endif
  554.     
  555.     val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
  556.  
  557.     if (val < 0)
  558.       perror_with_name (filename);
  559.  
  560.         text_start = N_TXTADDR (exec_aouthdr);
  561.         exec_data_start = N_DATADDR (exec_aouthdr);
  562.  
  563.     text_offset = N_TXTOFF (exec_aouthdr);
  564.     exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
  565.  
  566.     text_end = text_start + exec_aouthdr.a_text;
  567.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  568.     data_start = exec_data_start;
  569.     data_end += exec_data_start;
  570.  
  571.     fstat (execchan, &st_exec);
  572.     exec_mtime = st_exec.st_mtime;
  573.       }
  574. #endif /* not COFF_FORMAT */
  575.  
  576.       validate_files ();
  577.     }
  578.   else if (from_tty)
  579.     printf ("No exec file now.\n");
  580.  
  581.   /* Tell display code (if any) about the changed file name.  */
  582.   if (exec_file_display_hook)
  583.     (*exec_file_display_hook) (filename);
  584. }
  585.  
  586.